# coding=utf-8
import numpy as np
from scipy.spatial.distance import cdist
import requests
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
import pandas as pd

res = requests.get("http://192.168.211.89:8888/eUrbanGIS/home/gis/geosource/cluster?distance=50&partLayerIds=10254&gridLayerId=10391&gridLayerKeyValues=110112&girdLayerKeyField=distid&minpoints=1").json()
data_cluster = res["resultInfo"]["data"]["clusterInfo"]["10254"]



data = pd.read_csv("C:\\Users\\EDZ\\Desktop\\wholesale.csv")
data.drop(["Channel", "Region"], axis=1, inplace=True)

data = data[["Grocery", "Milk"]]
data = data.as_matrix().astype("float32", copy=False)#convert to array

#数据预处理，特征标准化，每一维是零均值和单位方差
stscaler = StandardScaler().fit(data)
data = stscaler.transform(data)

#画出x和y的散点图
plt.scatter(data[:, 0], data[:, 1])
plt.xlabel("Groceries")
plt.ylabel("Milk")
plt.title("Wholesale Data - Groceries and Milk")
plt.savefig("wholesale.png", format="PNG")

dbsc = DBSCAN(eps=0.5, min_samples=15).fit(data)

labels = dbsc.labels_ #聚类得到每个点的聚类标签 -1表示噪点
#print(labels)
core_samples = np.zeros_like(labels, dtype=bool) #构造和labels一致的零矩阵,值是false
core_samples[dbsc.core_sample_indices_] = True
#print(core_samples)


unique_labels = np.unique(labels)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels))) #linespace返回在【0,1】之间均匀分布数字是len个，Sepectral生成len个颜色


#print(zip(unique_labels,colors))
for (label, color) in zip(unique_labels, colors):
    class_member_mask = (labels == label)
    print(class_member_mask&core_samples)
    xy = data[class_member_mask & core_samples]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=color, markersize=10)

    xy2 = data[class_member_mask & ~core_samples]
    plt.plot(xy2[:, 0], xy2[:, 1], 'o', markerfacecolor=color, markersize=5)
plt.title("DBSCAN on Wholsesale data")
plt.xlabel("Grocery (scaled)")
plt.ylabel("Milk (scaled)")
plt.savefig("(0.9,15)dbscan_wholesale.png", format="PNG")